import { Injectable } from "@angular/core";
import { Config } from "./config";
import { Router } from "@angular/router";
import * as moment from 'moment';
@Injectable()
export class CommFuntion {
  constructor(private router: Router) { }

  /**
   * 将 bean中 json字符串 转为 json对象；
   */
  convertBean(bean: any = {}) {
    let beanC: any = {};
    for (var key in bean) {
      if (bean.hasOwnProperty(key)) {
        let value = bean[key] + "";
        let vin: any;
        vin = value;
        if (value.startsWith("{") && value.endsWith("}")) {
          vin = JSON.parse(value);
          if (vin["picUrl"]) {
            vin["picUrl"] = vin["picUrl"].split(",");
          }
        }
        beanC[key] = vin;
      }
    }
    return beanC;
  }

  /**
 * 将 bean中 图片总数计算出来
 */
  countPicNum(bean: any = {}) {
    let num: number = 0;
    for (var key in bean) {
      if (bean.hasOwnProperty(key)) {
        let value = bean[key];
        let vin: any;
        vin = value;
        if (value["picUrl"] != undefined) {
          if (vin["picUrl"]) {
            num += vin["picUrl"].length;
          }
        }
      }
    }
    return num;
  }

  /**
   * 将 bean中 json对象 转为 json字符串  ；
   */
  convertBeanToString(bean: any = {}) {
    let beanC = {};
    for (var key in bean) {
      if (bean.hasOwnProperty(key)) {
        let value = bean[key];
        let vin: any;
        vin = value;
        if (value["picUrl"] != undefined) {
          if (vin["picUrl"]) {
            vin["picUrl"] = vin["picUrl"].join(",");
          }
          vin = JSON.stringify(value);
        }
        beanC[key] = vin;
      }
    }
    return beanC;
  }

  toUpercase(key) {
    return key.replace(
      /\_+[a-zA-Z]/g,
      (str, index) => (index ? str.substr(-1).toUpperCase() : str)
    );
  }

  transTime(value) {
    if (!value) {
      return null;
    }
    let values = value + '';
    let date = values.substring(0, 8);
    let time = values.substring(8, 14);
    let dates = moment(date).format('YYYY-MM-DD');
    let times = time.substring(0, 2) + ':' + time.substring(2, 4) + ':' + time.substring(4, 6);

    return dates + ' ' + times;
  }

}
